/*
 * @Author: Ma Yuchen
 * @Date: 2022-10-29 20:40:10
 * @LastEditors: Ma YuChen
 * @LastEditTime: 2022-10-30 10:44:11
 * @Description: file content
 * @FilePath: \BMC\bsp_resetIrq.c
 */
#include "bsp_resetirq.h"
#include "gd32f4xx_gpio.h"              // GigaDevice::Device:StdPeripherals:GPIO
#include "gd32f4xx_exti.h"              // GigaDevice::Device:StdPeripherals:EXTI
#include "gd32f4xx_rcu.h"               // GigaDevice::Device:StdPeripherals:RCU

static uint32_t sys_reset = 0;

static void initResetGPIO(void);


void initResetExti(void)
{
	initResetGPIO();
	
	rcu_periph_clock_enable(RCU_SYSCFG);	//enable SYSCFG clock
	nvic_irq_enable(EXTI5_9_IRQn, 2,1);	//USE EXTI6
	syscfg_exti_line_config(EXTI_SOURCE_GPIOG, EXTI_SOURCE_PIN6);
}

void enableResetExti(void)
{
	exti_init(EXTI_6, EXTI_INTERRUPT,EXTI_TRIG_RISING);		//上边沿触发
	exti_interrupt_flag_clear(EXTI_6);
}

void disableResetExti(void)
{
	exti_interrupt_disable(EXTI_6);
}

static void initResetGPIO(void)
{
  rcu_periph_clock_enable(RCU_GPIOG);
	
    	/*GPIOG*/
	gpio_mode_set(GPIOG, GPIO_MODE_INPUT,GPIO_PUPD_NONE, LS7A_SYS_RESET_GPIO_PIN);
}

uint32_t getResetStatusFlag(void)
{
	return sys_reset;
}

void		setResetStatusFlag(uint32_t value)
{
	sys_reset=value;
}

void		clearResetStatusFlag(void)
{
	sys_reset=0;
}

void IQR_RESET_EVENT_FUNC(void)
{
	if(exti_interrupt_flag_get(EXTI_6)==SET)
	{
		exti_interrupt_flag_clear(EXTI_6);
		setResetStatusFlag(1);
	}
}
